package com.example.controller;

import com.example.entity.*;
import com.example.page.Page;
import com.example.service.*;
import com.example.utils.TokenUtils;
import com.example.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@RequestMapping("/product")
@RestController

public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SupplyService supplyService;
    @Autowired
    private PlaceService placeService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private UploadImgService uploadImgService;
    @Autowired
    private TokenUtils tokenUtils;
    @Value("${file.access-path}")
    private String fileAccessPath; // 访问图片的路径

    @RequestMapping("/product-page-list")
    public Result queryProductByConditionForPagination(Page page, Product product){
        // 执行根据条件查询商品并分页的业务方法
        productService.queryProductByConditionForPagination(page, product);

        // 响应结果，控制器参数page对象和业务方法参数page对象是同一个对象
        return Result.ok(page);
    }

    /**
     * 查询所有仓库接口url/product/store-list
     * @return
     */
    @RequestMapping("/store-list")
    public Result queryAllStore(){
        // 执行查询所有仓库的业务方法
        List<Store> storeList = storeService.queryAllStore();
        return Result.ok(storeList);
    }

    /**
     * 查询所有商品品牌接口url/product/brand-list
     * @return
     */
    @RequestMapping("/brand-list")
    public Result queryAllBrand(){
        List<Brand> brandList = brandService.queryAllBrand();
        return Result.ok(brandList);
    }

    /**
     * 查询所有的供应商接口url/product/supply-list
     * @return
     */
    @RequestMapping("/supply-list")
    public Result queryAllSupply(){
        List<Supply> supplyList = supplyService.queryAllSupply();
        return Result.ok(supplyList);
    }

    /**
     * 查询所有生产地的接口url/product/place-list
     * @return
     */
    @RequestMapping("/place-list")
    public Result queryAllPlace(){
        List<Place> placeList = placeService.queryAllPlace();
        return Result.ok(placeList);
    }

    /**
     * 查询所有物品规格单位接口url/product/unit-list
     * @return
     */
    @RequestMapping("/unit-list")
    public Result queryAllUnit(){
        // 执行查询所有物品规格单位的业务方法
        List<Unit> unitList = unitService.queryAllUnit();
        return Result.ok(unitList);
    }

    /**
     * 获取商品分类树接口url/product/category-tree
     * @return
     */
    @RequestMapping("/category-tree")
    public Result getProductTypeTree(){
        // 执行获取商品分类树的业务方法
        List<ProductType> productTypeTree = productTypeService.allProductTypeToTree();

        return Result.ok(productTypeTree);
    }

    /**
     * 完成上传的商品图片保存接口url/product/img-upload
     * @return
     */
    @CrossOrigin // 该接口url允许跨域请求
    @RequestMapping("/img-upload")
    public Result saveUploadProductFile(MultipartFile file){
        // 执行保存商品图片的业务方法
        Result result = uploadImgService.saveUploadProductImg(file);
        // 响应前端
        return result;
    }

    /**
     * 保存添加的商品的接口url/product/product-add
     * @param product
     * @param token
     * @return
     */
    @RequestMapping("/product-add")
    public Result saveProduct(@RequestBody Product product,
                              @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 执行根据商品编号查询商品的业务方法并判断商品是否存在
        if (productService.queryProductByProductNum(product.getProductNum()) != null) {// 商品存在
            return Result.err(Result.CODE_ERR_BUSINESS, "商品编号已存在！");
        }
        // 商品不存在

        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 定义访问该商品图片的相对路径
        String accessPath = fileAccessPath + product.getImgs();

        // 再次封装参数product
        product.setImgs(accessPath);
        product.setCreateBy(currentUser.getUserId());

        // 执行保存添加的商品的业务方法
        Result result = productService.saveProduct(product);

        // 响应结果
        return result;
    }

    /**
     * 修改商品状态接口url/product/state-change
     * @param product
     * @param token
     * @return
     */
    @RequestMapping("/state-change")
    public Result modifyProductStateById(@RequestBody Product product,
                          @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 封装product
        product.setUpdateBy(currentUser.getUserId());

        // 执行修改商品状态的业务方法并响应
        return productService.modifyProductStateById(product);
    }

    /**
     * 根据商品id删除商品接口url/product/product-delete/{id}
     * @param id
     * @return
     */
    @RequestMapping("/product-delete/{id}")
    public Result removeProductById(@PathVariable Integer id){
        // 执行删除商品的业务方法并响应
        return productService.removeProductById(id);
    }

    /**
     * 根据商品id修改商品信息接口url/product/product-update
     * @param product
     * @param token
     * @return
     */
    @RequestMapping("/product-update")
    public Result modifyProductById(@RequestBody Product product,
                           @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 判断商品的图片是否修改过，若有，则修改product的imgs属性的值
        if (!product.getImgs().startsWith(fileAccessPath)){//商品图片被修改过了
            // 定义新的图片名称
            String fileName = fileAccessPath + product.getImgs();
            product.setImgs(fileName);
        }

        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 对参数product进行数据封装
        product.setUpdateBy(currentUser.getUserId());

        // 执行修改商品信息的业务方法并响应
        return productService.updateProductById(product);
    }

}
